import requests
from selenium import webdriver
import time
import ddddocr
import re


headers = {
    "Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Referer": "https://passport.csdn.net/login?code=applets",
    "Sec-Fetch-Dest": "image",
    "Sec-Fetch-Mode": "no-cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Google Chrome\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "uuid_tt_dd": "10_3952825040-1752026931774-231242",
    "dc_session_id": "10_1752026931774.990426",
    "c_pref": "default",
    "c_ref": "default",
    "fid": "20_34124393829-1752026932931-308893",
    "c_first_ref": "default",
    "c_first_page": "https%3A//www.csdn.net/",
    "c_dsid": "11_1752026932932.152378",
    "c_segment": "10",
    "c_page_id": "default",
    "log_Id_pv": "1",
    "c-sidebar-collapse": "0",
    "creative_btn_mp": "1",
    "Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac": "1752026935",
    "Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac": "1752026935",
    "HMACCOUNT": "184D75C133F6BDFD",
    "dc_sid": "617fa052467f23825e980891ff6d6f9c",
    "c_ab_test": "1",
    "hide_login": "1",
    "log_Id_click": "1",
    "bc_bot_session": "17520269433f7d8f59672334e7",
    "SESSION": "0dc01d74-0469-4be6-bf88-18ed13ff38c6",
    "log_Id_view": "59",
    "https_waf_cookie": "a9f9f000-2e75-4d4bd55b248c23086178f04ba1df15fcf03f",
    "bc_bot_token": "10017520269433f7d8f59672334e7023d7e",
    "bc_bot_rules": "-",
    "bc_bot_fp": "9ba9fbe30422c9e309dd1b490413d71c",
    "dc_tos": "sz40nd",
    "waf_captcha_marker": "83c96f1d27a7a41cc1a364f10e7094765cec6c806b946f29081aed3f58723081"
}


options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.get("https://www.csdn.net/")
driver.maximize_window()
time.sleep(3)

driver.find_element("xpath", "//a[@class='toolbar-btn-loginfun']").click()
time.sleep(5)

driver.switch_to.frame(driver.find_element("xpath", "//div[@id='passportbox']/iframe"))
driver.find_element("xpath", "//div[@class='login-box-tabs-items']/span[3]").click()
time.sleep(5)
driver.find_element("xpath", "//div[@class='base-input base-select-input']/input[@class='base-input-text']").send_keys("15716726115")
driver.find_element("xpath", "//div[@class='base-input']/input[@class='base-input-text']").send_keys("236775")
driver.find_element("xpath", "//div[@class='login-form-item']/button[@class='base-button']").click()
time.sleep(5)

# 获取图片链接并下载
# 使用XPath定位元素
element = driver.find_element("xpath", "//div[@class='verify-img-panel']")
# 获取style属性值
style_value = element.get_attribute('style')
# print(style_value)
url = re.search(r'background: url\(["\']?(/cdn_cgi_bs_captcha/static/images/slides/[a-zA-Z0-9-]+\.[a-zA-Z0-9]+)["\']?\)', style_value)
picture_url = "https://passport.csdn.net" + str(url.group(1))
print(picture_url)
response = requests.get(picture_url, headers=headers, cookies=cookies)
with open("quantu.png", "wb") as f:
    f.write(response.content)

# 获取小图片链接
small_element = driver.find_element("xpath", "//div[@class='verify-sub-block']")
# 获取style属性值
style_value = small_element.get_attribute('style')
# print(style_value)
url = re.search(r'background-image: url\(["\']?(/cdn_cgi_bs_captcha/static/images/slides/[a-zA-Z0-9-]+-[a-zA-Z0-9-]+-[a-zA-Z0-9-]+-[a-zA-Z0-9-]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+\.\w+)["\']?\)', style_value)
small_picture = "https://passport.csdn.net" + str(url.group(1))
print(small_picture)
# 获取小块图片
response = requests.get(picture_url, headers=headers, cookies=cookies)
with open("xiaokuai.png", "wb") as f:
    f.write(response.content)
#
# det = ddddocr.DdddOcr(det=False, ocr=False)
#
# with open('target.png', 'rb') as f:
#     target_bytes = f.read()
#
# with open('background.png', 'rb') as f:
#     background_bytes = f.read()
#
# res = det.slide_match(target_bytes, background_bytes)
#
# print(res)
